package com.qianfeng.wang.guangjie2.activity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qianfeng.wang.guangjie2.R;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



    private long mkeyTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if((System.currentTimeMillis() - mkeyTime) > 2000){
                mkeyTime = System.currentTimeMillis();
                //自定义Toast
                LayoutInflater inflater=getLayoutInflater();
                View view= inflater.inflate(R.layout.item_main_toast, (ViewGroup) findViewById(R.id.toast_1));

                Toast toast=new Toast(getApplicationContext());
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(view);
                toast.show();


//                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            }else{
                finish();
            }

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


}
